package com.example.meterialproject.view_models

import android.view.Menu
import android.view.MenuItem
import androidx.core.view.forEach
import androidx.lifecycle.ViewModel
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.LottieDrawable
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView

/**
 *
 * @ClassName: NavigationBottomViewModel
 * @Author: android 超级兵
 * @CreateDate: 1/24/22$ 7:32 PM$
 * TODO
 */
class NavigationBottomViewModel : ViewModel() {
    // 注意 最多能放5个！！(源码规定)
    private val pairAnimationList = listOf(
        "tissue.json" to "首页",
        "chemical.json" to "通讯录",
        "cow.json" to "朋友圈",
        "astronaout.json" to "我的",
    )

    private var mCurrentPosition = 0

    fun initBottomNavigationView(bottomNav: BottomNavigationView) {
        bottomNav.menu.apply {
            for (i in pairAnimationList.indices) {
                // 添加title
                add(Menu.NONE, i, Menu.NONE, pairAnimationList[i].second)

                // 设置lottie
                setLottieDrawable(pairAnimationList, bottomNav, i)
            }
        }
        // 初始化点击监听
        initClickEvent(bottomNav)
    }

    private fun initClickEvent(bottomNav: BottomNavigationView) {
        // 监听选中事件
        bottomNav.setOnItemSelectedListener(getOnNavigationItemSelectedListener(bottomNav))
        bottomNav.setOnItemReselectedListener {
            getOnNavigationItemReselectedListener(
                bottomNav
            )
        }

        // 默认选中第一个
        bottomNav.selectedItemId = 0

        // 禁止长按弹出 拦截长按事件
        bottomNav.menu.forEach {
            val menuItemView = bottomNav.findViewById<BottomNavigationItemView>(it.itemId)
            menuItemView.setOnLongClickListener {
                true
            }
        }
    }

    // 监听变化
    // Listener for handling selection events on bottom navigation items.
    // 事件监听器来处理选择底部导航项。
    private fun getOnNavigationItemSelectedListener(lottieBottomNavView: BottomNavigationView) =
        NavigationBarView.OnItemSelectedListener { item ->
            handleNavigationItem(item, lottieBottomNavView)
            true
        }

    // Listener for handling reselection events on bottom navigation items
    // 用于处理底部导航项上的重新选择事件的侦听器
    private fun getOnNavigationItemReselectedListener(lottieBottomNavView: BottomNavigationView) =
        NavigationBarView.OnItemReselectedListener { item ->
            handleNavigationItem(item, lottieBottomNavView)
        }

    /*
   * 作者:android 超级兵
   * 创建时间: 1/24/22 7:46 PM
   * TODO 将lottie转变为drawable 设置给menu
   */
    private fun Menu.setLottieDrawable(
        lottieAnimationList: List<Pair<String, String>>,
        bottomNav: BottomNavigationView,
        position: Int
    ) {
        // 转变为lottieDrawable
        findItem(position).icon =
            replaceLottieDrawable(lottieAnimationList[position].first, bottomNav)
    }


    /*
     * 作者:android 超级兵
     * 创建时间: 1/26/22 1:50 PM
     * TODO 选中监听
     */
    private fun handleNavigationItem(item: MenuItem, bottomNav: BottomNavigationView) {
        handlePlayLottieAnimation(item, bottomNav)
        mCurrentPosition = item.itemId
    }

    private fun handlePlayLottieAnimation(item: MenuItem, bottomNav: BottomNavigationView) {
        val currentIcon = item.icon as? LottieDrawable
        currentIcon?.apply {
            // 开启动画
            playAnimation()

            // 无限播放
//            loop(true)
        }

        // 处理 tab 切换，icon 对应调整
        if (item.itemId != mCurrentPosition) {
            bottomNav.menu.findItem(mCurrentPosition).icon =
                replaceLottieDrawable(
                    pairAnimationList[mCurrentPosition].first,
                    bottomNav
                )
        }
    }

    /**
     * 替换成 LottieDrawable
     */
    private fun replaceLottieDrawable(
        jsonKey: String,
        bottomNavigationView: BottomNavigationView
    ): LottieDrawable {
        return LottieDrawable().apply {
            val result = LottieCompositionFactory.fromAssetSync(
                // 加载lottie 数据
                bottomNavigationView.context.applicationContext, jsonKey
            )
            callback = bottomNavigationView

            // 设置动画
            composition = result.value
        }
    }
}
